
import 'package:get/get.dart';
import 'package:xim/common/get_style_view.dart';
import 'package:xim/common/util/datetime.dart';
import 'package:xim/common/util/system.dart';
import 'package:xim/config/store_config.dart';
import 'package:xim/http/apis/session/session_api.dart';
import 'package:xim/http/apis/session/session_model.dart';
import 'package:xim/http/apis/user/user_model.dart';

class ChatListController extends GetxStateMixinController<List<SessionMessageModel>> {

  /// 会话列表
  var sessionList = <SessionMessageModel>[].obs;

  /// 用户消息
  late UserInfoModel user;

  /// 是否展示应用密码提示弹框
  bool isShowPasswordDialog=false;

  @override
  Future<List<SessionMessageModel>> dataInit() async {
    // 消息列表数据
    var sessionListData = await SessionApi.requestSessionList();
    isShowPasswordDialog = await StoreApi.instance.secureTakeAppPassword()==null;
    sessionList.value = sessionListData;
    return sessionListData;
  }

  String formatDate(DateTime dateTime) {
    var nowDateTime = DateTime.now();
    var days = nowDateTime.day - dateTime.day;
    var month = nowDateTime.month - dateTime.month;
    var year = nowDateTime.year - dateTime.year;

    if (year > 0) {
      // 跨年
      return dateTime.format("yyyy年M月d日");
    }
    if (month > 0) {
      // 跨月
      return dateTime.format("M月d日");
    }
    if (days > 0) {
      // 跨天
      return dateTime.format("M月d日");
    }
    return dateTime.format("HH:mm");
  }

  /// 关闭 应用密码提示弹框
  closePasswordDialog(){
    isShowPasswordDialog=false;
    update();
  }

}

